package com.mypack.crm.controller.customer;

import com.github.pagehelper.PageInfo;
import com.mypack.crm.annotation.ResponseResult;
import com.mypack.crm.constants.EnumDeleteFlag;
import com.mypack.crm.constants.EnumResponse;
import com.mypack.crm.dto.customer.CustomerDTO;
import com.mypack.crm.dto.customer.CustomerSearchDTO;
import com.mypack.crm.service.customer.CustomerService;
import com.mypack.crm.utils.Result;
import com.mypack.crm.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/query")
    @ResponseResult
    public CustomerDTO query(Integer id) {
        CustomerDTO customerDTO = customerService.queryById(id);
        return customerDTO;
    }

    @RequestMapping("/list")
    public Result list(@RequestBody CustomerSearchDTO searchDTO) {

        List<CustomerDTO> list = customerService.list(searchDTO, true);

        return ResultUtils.success(new PageInfo<>(list));
    }

    @RequestMapping("/add")
    public Result add(@RequestBody CustomerDTO customerDTO) {

        customerService.add(customerDTO);

        return ResultUtils.success();
    }


    @RequestMapping("/update")
    public Result update(@RequestBody CustomerDTO customerDTO) {

        if(customerDTO.getId() == null){
            return ResultUtils.fail(EnumResponse.INVALID_PARAM);
        }

        customerService.update(customerDTO);

        return ResultUtils.success();
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {


        CustomerDTO customerDTO = new CustomerDTO();
        customerDTO.setId(id);
        customerDTO.setDeleteFlag(EnumDeleteFlag.YES.getCode());

        customerService.update(customerDTO);

        return ResultUtils.success();
    }

}
